package rexsee.cartoon;

import rexsee.cartoon.component.Cartoon;
import rexsee.cartoon.component.Scrolling;
import rexsee.core.application.RexseeApplication;
import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.browser.clazz.LogListeners.LogListener;
import rexsee.core.lang.RexseeLanguage;
import android.app.Activity;
import android.content.Context;
import android.os.Looper;

public class RexseeCartoon implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Cartoon";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	private final Context mContext;
	private final Browser mBrowser;
	private CartoonDialog mDialog = null;

	private String mEncoding = "UTF-8";

	public RexseeCartoon(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	public void open(String url, String style) {

		dismiss();
		mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
		String content = mBrowser.urlListeners.getContentWithEncoding(mBrowser.urlListeners.getAbsoluteUrl(url), mEncoding);
		if (content == null || content.equalsIgnoreCase(RexseeApplication.EXCEPTION_PREFIX)) {
			mBrowser.application.alert(RexseeLanguage.MESSAGE_WEAK_NETWORK);
		} else {
			openContent(content, style);
		}

	}
	public void openContent(final String xmlContent, final String style) {
		LogListener logListener = new LogListener() {
			@Override
			public void run(Context context, Browser browser, int type, String message) {
				mBrowser.logListeners.run(context, browser, type, message);
			}
		};
		final Parser mParser = new Parser(mBrowser, logListener);
		mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
		new Thread() {
			@Override
			public void run() {
				try {
					Looper.prepare();
				} catch (Exception e) {
				}
				mParser.parse(xmlContent);
				((Activity) mContext).runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mBrowser.progressDialog.hide();
						mDialog = new CartoonDialog(mBrowser, style);
						mDialog.setParser(mParser);
					}
				});
			}
		}.start();
	}

	private Action _getAction(String actionId) {
		if (mDialog == null || mDialog.parser == null) return null;
		return mDialog.parser.actions.getAction(actionId);
	}
	private Cartoon _getCartoon(String id) {
		if (mDialog == null || mDialog.parser == null) return null;
		return mDialog.parser.cartoons.get(id);
	}

	//JavaSript Interface

	public String getEncoding() {
		return mEncoding;
	}
	public void setEncoding(String encoding) {
		mEncoding = encoding;
	}
	public void dismiss() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
	}

	//Action operation 

	public int getActionCount() {
		if (mDialog == null || mDialog.parser == null) return -1;
		return mDialog.parser.actions.size();
	}
	public boolean actionExists(String actionId) {
		return _getAction(actionId) != null;
	}
	public void releaseAction(String actionId) {
		Action action = _getAction(actionId);
		if (action != null) action.destroy();
	}
	public void removeAction(String actionId) {
		if (mDialog == null || mDialog.parser == null) return;
		mDialog.parser.actions.removeAction(actionId);
	}

	public int getFrameCount(String actionId) {
		Action action = _getAction(actionId);
		if (action == null) return 0;
		else return action.size();
	}
	public void removeFrame(String actionId, int frameIndex) {
		Action action = _getAction(actionId);
		if (action == null) return;
		Frame frame = action.getFrame(frameIndex);
		if (frame != null) {
			action.removeFrame(frameIndex);
			frame.destroy();
		}
	}

	//Cartoon operation

	public int getCount() {
		if (mDialog == null || mDialog.parser == null) return -1;
		return mDialog.parser.cartoons.size();
	}
	public boolean exists(String id) {
		return _getCartoon(id) != null;
	}
	public void remove(String id) {
		if (mDialog == null || mDialog.parser == null) return;
		mDialog.parser.cartoons.remove(id);
	}

	public void play(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon == null) return;
		cartoon.play = true;
	}
	public void stop(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon == null) return;
		cartoon.play = false;
	}
	public boolean isPlaying(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.play;
	}

	public void show(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.visible = true;
	}
	public void hide(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.visible = false;
	}
	public boolean isShowing(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.visible;
	}

	public void setBlockTouchEnabled(String id, boolean enabled) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.blockTouch = enabled;
	}
	public boolean isBlockTouchEnabled(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.blockTouch;
	}

	public void setBlockMoveEnabled(String id, boolean enabled) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.blockMove = enabled;
	}
	public boolean isBlockMoveEnabled(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.blockMove;
	}

	public void setBlockedReportEnabled(String id, boolean enabled) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.blockedReport = enabled;
	}
	public boolean isBlockedReportEnabled(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.blockedReport;
	}

	public void setDragable(String id, boolean dragable) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.dragable = dragable;
	}
	public boolean isDragable(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.dragable;
	}

	public void setClickable(String id, boolean clickable) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.clickable = clickable;
	}
	public boolean isClickable(String id) {
		Cartoon cartoon = _getCartoon(id);
		return (cartoon == null) ? false : cartoon.clickable;
	}

	public void setMatrix(String id, String matrix) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.matrixStr = matrix;
	}
	public void setClip(String id, String clip) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.clip = Actions.getPath(clip);;
	}
	public void setClipBorder(String id, String clipBorder) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.clipBorder = Actions.getBorder(clipBorder);;
	}

	public void setDirection(String id, String direction) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null && cartoon instanceof Scrolling) ((Scrolling) cartoon).setDirection(direction);
	}
	public void setCenter(String id, int x, int y) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null && cartoon instanceof Scrolling) ((Scrolling) cartoon).setCenter(x, y);
	}
	public void setStep(String id, float step) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null && cartoon instanceof Scrolling) ((Scrolling) cartoon).step = step;
	}
	public void setThreshold(String id, float threshold) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null && cartoon instanceof Scrolling) ((Scrolling) cartoon).threshold = threshold;
	}

	//Change action

	public String getActionId(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon == null) return "";
		return cartoon.getAction();
	}
	public String getPressedActionId(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon == null) return "";
		return cartoon.getPressedAction();
	}
	public void setAction(String id, String actionId) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.setAction(actionId);
	}
	public void setPressedAction(String id, String actionId) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.setPressedAction(actionId);
	}
	public void clearPressedAction(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.setPressedAction(null);
	}

	//Change position

	public String getPosition(String id) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon == null) return "";
		Position position = cartoon.getPosition();
		if (position == null) return "";
		String rtn = "{";
		rtn += "\"left\":" + position.rect.left + ",";
		rtn += "\"top\":" + position.rect.top + ",";
		rtn += "\"right\":" + position.rect.right + ",";
		rtn += "\"bottom\":" + position.rect.bottom + ",";
		rtn += "\"zIndex\":" + position.z_index;
		rtn += "}";
		return rtn;
	}
	public void setPosition(String id, int left, int top, int right, int bottom) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.setPosition(left, top, right, bottom);
	}
	public void setZIndex(String id, int zIndex) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.setZIndex(zIndex);
	}
	public void moveToByStepSize(String id, int left, int top, int stepSize) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.moveToToByStepSize(left, top, stepSize);
	}
	public void moveToByStepNumber(String id, int left, int top, int stepNumber) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.moveToToByStepNumber(left, top, stepNumber);
	}
	public void moveTo(String id, String positions) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.moveTo(positions);
	}
	public void moveTo(String id, int left, int top) {
		Cartoon cartoon = _getCartoon(id);
		if (cartoon != null) cartoon.moveTo(left, top);
	}

}
